<template>
  <div>
    <el-drawer
      title="分配金额"
      :before-close="handleClose"
      :visible.sync="distributionAmtDialog"
      direction="rtl"
      custom-class="add-drawer"
      ref="drawer"
      size="30%"
      label-position="top"
    >
      <div class="demo-drawer__content">
        <el-form :model="ruleForm" :rules="rules" ref="ruleFormRef">
          <p>主卡可分配金额</p>
          <h3>{{ parentCardInfo.balance || 0 }} 元</h3>
          <el-form-item label="子卡分配金额" prop="money">
            <div style="width: 100%">
              <el-input v-model="ruleForm.money" autocomplete="off"></el-input>
              <span>元</span>
            </div>
          </el-form-item>
        </el-form>
        <div class="demo-drawer__footer">
          <el-button @click="handleClose">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </div>
      </div>
    </el-drawer>
  </div>
</template>
<script>
import { getParentCardInfoApi } from "@/api/teamCard/teamCardList";
import { allocationSonCardAmountApi } from "@/api/teamCard/sonCard";
export default {
  name: "DistributionAmt",
  props: {
    distributionAmtDialog: {
      type: Boolean,
      default: false,
    },
    currentData: {
      type: Object,
      default: () => {},
    },
  },
  data() {
    // 充值金额
    let checkSonCardAmt = (rule, value, callback) => {
      let amount = Number(value.trim());
      const reg = /^\d+(\.\d{1,2})?$/;
      if (!value) return callback(new Error("请输入金额"));
      if (value <= 0) return callback(new Error("金额不能小于等于0"));
      if (Number.isNaN(amount)) return callback(new Error("请输入合法数字"));
      if (!reg.test(amount)) return callback(new Error("金额最多保留两位小数"));
      callback();
    };
    return {
      amt: 1000,
      ruleForm: {
        money: "",
      },
      parentCardInfo: {},
      rules: {
        money: [
          { required: true, validator: checkSonCardAmt, trigger: "blur" },
        ],
      },
    };
  },

  watch: {
    distributionAmtDialog(val) {
      if (val) {
        this.getParentCardInfo();
      }
    },
  },

  methods: {
    // 获取主卡信息
    getParentCardInfo() {
      let parentId = this.currentData.fleetCardId;
      getParentCardInfoApi(parentId).then((res) => {
        this.parentCardInfo = res.data;
      });
    },
    handleClose() {
      this.$emit("update:distributionAmtDialog", false);
      this.resetForm("ruleFormRef");
    },
    handleSubmit() {
      this.$refs.ruleFormRef.validate((valid) => {
        if (valid) {
          console.log("提交");
          const params = {
            allocationAmount: this.ruleForm.money,
            id: this.currentData.id,
          };
          allocationSonCardAmountApi(params).then((res) => {
            this.msgSuccess("分配成功");
            this.handleClose();
            this.$emit("reloadData");
          });
        } else {
          console.log("error submit");
        }
      });
    },
  },
};
</script>
<style scoped lang="scss">
::v-deep .add-drawer {
  .el-drawer__header {
    padding-bottom: 20px;
    border-bottom: 1px solid #ddd;
  }
  .el-drawer__body {
    padding: 20px;
    .demo-drawer__content {
      width: 100%;
      .demo-drawer__footer {
        text-align: end;
      }
    }
  }
  .el-form-item {
    margin-bottom: 22px;
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    .el-form-item__content {
      div {
        display: flex;
        span {
          margin-left: 10px;
        }
      }
      .demo-drawer__footer {
        text-align: end;
      }
    }
  }
}
</style>
